package com.wk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.entity.User;
import com.wk.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wangkun
 * @since 2019-05-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "获取用户列表", notes = "获取所有的用户信息")
    @GetMapping("/users")
    public List<User> getUser()
    {
        return userService.list();
    }

    @ApiOperation(value = "新增用户", notes = "根据User对象创建新用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping("/")
    public boolean addUser(User user)
    {
        return userService.save(user);
    }


    @ApiOperation(value = "删除用户", notes = "根据用户id删除用户")
    @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "int")
    @DeleteMapping("/{userid}")
    public boolean deleteUser(@PathVariable(value = "userid") int userid)
    {
        return userService.removeById(userid);
    }

    @ApiOperation(value = "查询用户", notes = "根据用户id查询用户")
    @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "int")
    @GetMapping("/{userid}")
    public User getUser(@PathVariable(value = "userid") int userid)
    {
        return userService.getById(userid);
    }

    @ApiIgnore
    @GetMapping("/test")
    public Object test(){
        User user = userService.getById(1);
        IPage<User> userIPage = userService.page(new Page<User>(1,10),new QueryWrapper<>());

        return userIPage;


    }

    @GetMapping("/get")
    public Map<String,Object> getUserByCondition(@RequestParam("name") String UserName)
    {
        Map<String,Object> r = new HashMap<>();
        QueryWrapper<User>  userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userName",UserName);
        r.put("user",userService.getOne(userQueryWrapper));
        return r;
    }

}

